package cn.hdu.liang.ch2;

public class DeadlockDemo {
    private final Object lock1 = new Object();
    private final Object lock2 = new Object();

    public void instanceMethod1() {
        synchronized(lock1) {
            synchronized(lock2) {
                System.out.println("实例方法1的第一个线程");
            }
        }
    }

    public void instanceMethod2() {
        synchronized(lock2) {
            synchronized(lock1) {
                System.out.println("实例方法2的第二个线程");
            }
        }
    }

    public static void main(String[] args) {
        final DeadlockDemo dld = new DeadlockDemo();
        Runnable r1 = ()->{
            while(true) {
                dld.instanceMethod1();
                try {
                    Thread.sleep(50);
                } catch(InterruptedException ie) {

                }
            }
        };
        Thread thdA = new Thread(r1);

        Runnable r2 = ()->{
            while(true) {
                dld.instanceMethod2();
                try {
                    Thread.sleep(50);
                } catch(InterruptedException ie) {

                }
            }
        };
        Thread thdB = new Thread(r2);

        thdA.start();
        thdB.start();
    }
}
